package com.example.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;


public class MyButton extends View {
    public enum State {
        NORMAL,
        DISABLED,
        LOADING,
    }
    private State currentState  = State.NORMAL;
    //画笔
    private Paint normalPaint = new Paint();
    private Paint disabledPaint = new Paint();
    private Paint loadingPaint = new Paint();
    Paint paint = new Paint();
    Path path  =new Path();




    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array= context.obtainStyledAttributes(attrs,R.styleable.MyButton);
        array.recycle();

        //图片自动旋转
//        RotateAnimation rotateAnim = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        rotateAnim.setRepeatCount(Animation.INFINITE);
//        rotateAnim.setDuration(2000);
//        rotateAnim.setInterpolator(new LinearInterpolator());
        // tv.startAnimation(rotateAnim)
    }


    public void setState(State state) {
        currentState = state;
        invalidate();
    }
    public State getState() {
        return currentState;
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
 //       paint.setStrokeWidth(5);
        //画一个圆
 //       canvas.drawCircle(300,310,150,paint);
        //画一个矩形
 //       canvas.drawRect(10,10,100,100,paint);

//画三角形
//        path.moveTo(10,10);
//        path.lineTo(10,100);
//        path.lineTo(300,100);
//        path.close();
        //画弧形
//        path.moveTo(10,10);
//        RectF rectF = new RectF(100,10,200,100);
//        path.arcTo(rectF,0,180,true);

        //创建区域
//        Region region = new Region(new Rect(50,50,200,100));
//        drawRegion(canvas,region,paint);

        //创建一个椭圆用正方形截取
        RectF rect = new RectF(50,50,200,500);
        path.addOval(rect,Path.Direction.CCW);
        Region rgn = new Region();
        rgn.setPath(path,new Region(50,50,200,200));
        //取交集
//        rgn.union(Rect())
//        rgn.op()



//        drawRegion(canvas,rgn,paint);


//        //获取自身View宽高
//        int width = getWidth();
//        int height = getHeight();
//
//        switch (currentState){
//            case NORMAL:
//                canvas.drawRect(0,0,0,0,normalPaint);
//                break;
//            case DISABLED:
//                canvas.drawRect(0,0,width,height,disabledPaint);
//                break;
//            case LOADING:
//                canvas.drawRect(0,0,width,height,loadingPaint);
//                break;

        }

        // 画区域
//    private void drawRegion(Canvas canvas, Region region, Paint paint) {
//        RegionIterator iter = new RegionIterator(region);
//        Rect r = new Rect();
//        while (iter.next(r)){
//            canvas.drawRect(r,paint);
//        }
//
//    }
    //构建画笔方法
    private Paint generatePaint(int color,Paint.Style style,int width){
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(width);
        return paint;
    }





//        //设置字体颜色
//        Paint textPaint = new Paint();
//        textPaint.setColor(Color.WHITE);
//        textPaint.setTextSize(50);
//        //绘制字体
//        Rect bounds = new Rect();
//        textPaint.getTextBounds(buttonText, 0, buttonText.length(), bounds);
//        int x = (width - bounds.width()) / 2;
//        int y = (height + bounds.height()) / 2;
//        canvas.drawText(buttonText, x, y, textPaint);
//    }
}
